#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"
#include <iostream>
#include <unordered_map>

class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<pxt::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<pxt::AddRequest> AddRequestPtr;
    Server(int port)
        :_server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port),
            "Server", muduo::net::TcpServer::kReusePort)
        ,_dispatcher(std::bind(&Server::onUnknowMessage, this, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<pxt::TranslateRequest>(std::bind(&Server::onTranslate, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<pxt::AddRequest>(std::bind(&Server::onAdd, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3));
    }
    void start()
    {
        _server.start();
        _baseloop.loop();
    }
private:
    std::string translate(const std::string &str)
    {
        std::unordered_map<std::string, std::string> dict_map = {
            {"hello", "你好"},
            {"signal", "信号"},
            {"connect", "连接"},
            {"lib", "库"}
        };
        auto it = dict_map.find(str);
        if(it == dict_map.end())
        {
            return "未知单词";
        }
        return it->second;
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int req_num1 = message->num1();
        int req_num2 = message->num2();
        int rsp_num = req_num1 + req_num2;
        pxt::AddResponse resp;
        resp.set_result(rsp_num);
        _codec.send(conn, resp);
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestPtr &message, muduo::Timestamp)
    {
        // 1.提取有效内容
        std::string req_msg = message->msg();
        // 2.进行翻译得到结果
        std::string rsp_msg = translate(req_msg);
        // 3.组织protobuf响应
        pxt::TranslateResponse resp;
        resp.set_msg(rsp_msg);
        // 4.发送响应
        _codec.send(conn, resp);
    } 
    void onUnknowMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknowMessage" << message->GetTypeName();
        conn->shutdown();
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            LOG_INFO << "新连接建立成功";
        }
        else
        {
            LOG_INFO << "新连接建立断开";
        }
    }
private:
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server; // 服务器对象
    ProtobufDispatcher _dispatcher; // 请求分发器对象 -- 注册处理函数
    ProtobufCodec _codec; // protobuf协议处理器 -- 针对收到的请求数据进行protobuf协议处理
};

int main()
{
    Server server(8080);
    
    server.start();

    return 0;
}